package main

import (
	"gorm.io/gen"
	"gorm.io/gorm"
	"mall_frontend_api/biz/config"
	"mall_frontend_api/biz/dal/db"
	"mall_frontend_api/cmd/method"
)

func main() {
	config.MustInit()
	db.MustInit()

	g := gen.NewGenerator(gen.Config{
		FieldSignable: false,
		Mode:          gen.WithDefaultQuery,
		OutPath:       "biz/dal",
		ModelPkgPath:  "biz/model",
	})
	g.UseDB(db.GetDB())

	g.WithDataTypeMap(map[string]func(detailType gorm.ColumnType) (dataType string){
		"int": func(detailType gorm.ColumnType) string {
			return "int64"
		},
		"tinyint": func(detailType gorm.ColumnType) string {
			return "int64"
		},
	})

	g.ApplyInterface(func() {},
		g.GenerateModel("user",
			gen.FieldType("last_login_time", "gorm.DeletedAt"),
		),
		g.GenerateModel("product",
			gen.FieldType("status", "common.ProductStatus"),
			gen.FieldType("spec_type", "common.ProductSpecType"),
		),
		g.GenerateModel("category"),
		g.GenerateModel("cart"),
		g.GenerateModel("region"),
		g.GenerateModel("user_address"),
		g.GenerateModel("product_item"),
		g.GenerateModel("product_spec"),
		g.GenerateModel("product_spec_value"),
		g.GenerateModel("contact_us_option"),
		g.GenerateModel("user_points_log",
			gen.FieldType("type", " common.UserPointsType"),
		),
		g.GenerateModel("user_report",
			gen.FieldType("status", "common.UserReportStatus")),
		g.GenerateModel("orders",
			gen.FieldType("status", "common.OrderStatus"),
			gen.FieldType("pay_status", "common.PayStatus"),
			gen.FieldType("submit_time", "*time.Time"),
			gen.FieldType("pay_time", "*time.Time"),
			gen.FieldType("receiver_time", "*time.Time"),
			gen.FieldType("delivery_time", "*time.Time"),
			gen.FieldType("cancel_time", "*time.Time"),
			gen.FieldType("pay_type", "common.PaymentType"),
		),
		g.GenerateModel("orders_item",
			gen.FieldType("status", "common.OrderStatus"),
		),
	)

	var allTableNames []string
	for tableName, _ := range g.Data {
		if err := method.GenerateExtraMethod(tableName, g.OutPath); err != nil {
			panic(err)
		}
		allTableNames = append(allTableNames, tableName)
	}

	if err := method.GenInitDAO(allTableNames, g.OutPath); err != nil {
		panic(err)
	}
	g.Execute()
}
